import axios from "axios";
import PositionModule from "./position";

export default {
  state: {
    userInfos: [],
    curUserInfos: [],
    usersLength: 0,
  },
  getters: {
    getUsersLength(state) {
      return state.usersLength;
    },
    setUsersLength(state, usersLength) {
      state.usersLength = usersLength;
    },
  },
  mutations: {
    getUserInfos() {},
    addUsersLength(state) {
      state.usersLength++;
    },
  },
  actions: {
    // 查 分页查询
    getCurUserInfos(context, data) {
      let url = "/user/info/select/page";
      let postData = {
        pageSize: data.pageSize,
        curPageNum: data.curPageNum,
      };
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => {
          context.state.curUserInfos = resp.data.data;
          data.success(resp);
        })
        .catch((error) => {
          console.log(error); // 或者进行其他错误处理操作
        });
    },
    // 查 总用户信息数量
    getUserInfosLength(context, token) {
      axios
        .post(
          "/user/info/get/length",
          {},
          {
            headers: {
              Authorization: "Bearer " + token,
            },
          }
        )
        .then((resp) => {
          context.state.usersLength = resp.data.data;
        })
        .catch((resp) => console.log(resp));
    },
    // 删
    deleteUserInfoById(context, data) {
      let url = "/user/info/delete/by/id";
      let postData = {
        id: data.id,
      };
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => {
          console.log(resp);
          data.success(resp);
        })
        .catch((resp) => {
          console.log(resp);
          data.error(resp);
        });
    },
    // 改
    updateUserInfo(context, data) {
      let url = "/user/info/update";
      let postData = data.userInfo;
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => {
          data.success(resp);
        })
        .catch((resp) => {
          data.error(resp);
        });
    },
    // 增
    addUserInfo(context, data) {
      let url = "/user/info/add";
      let postData = data.userInfo;
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => {
          data.success(resp);
        })
        .catch((resp) => {
          data.error(resp);
        });
    },
    // 修改个人信息
    updatePersonalInfo(context, data) {
      let url = "/user/info/update/personal/info";
      let postData = data.userInfo;
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => {
          data.success(resp);
        })
        .catch((resp) => {
          data.error(resp);
        });
    },
  },
  modules: {
    position: PositionModule,
  },
};
